var paginationTotal = 0;//记录总数
$(function(){
    $(".messageDiv").dialog({
		autoOpen:false,
		width:400,
		height:200,
		modal:true,
		show:"slide",
        zIndex:200,
        title:"Notice",
        buttons:{
            "Close": function() {$(this).dialog('close');}
        }
	});
	 $(".flushMessageDiv").dialog({
		autoOpen:false,
		width:400,
		height:200,
		modal:true,
		show:"slide",
        zIndex:200,
        title:"Notice",
        buttons:{
            "Close": function() {$(this).dialog('close');}
        },
		close:function(){
			window.location.reload();
		}
	});
	
    $("#confrimDiv").dialog({
		autoOpen:false,
		width:400,
		height:200,
		modal:true,
		show:"slide",
        zIndex:200,
        title:"提示信息"
	});

    $(".prdouctDiv").dialog({
		autoOpen:false,
		width:900,
		height:400,
		modal:true,
		show:"slide",
        zIndex:200,
        buttons:{
            "Close": function() {$(this).dialog('close');}
        }
	});
});

function OpenProductDiv(){
    $(".prdouctDiv").dialog('open');
    initData(0);
}

function warehouseProduct(){
    initData(0);
}
function loadData(page,pageSize){

    product_title = $("#searchForm [name=product_title]").val();
    product_sku = $("#searchForm [name=product_sku]").val();
    warehouse_id = $("#editForm [name=warehouse_id]").val();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/merchant/warehouse-products/index",
        data:{
            'page':page,
            'pageSize':pageSize,
            'product_sku':product_sku,
            'product_status':1,
            'product_title':product_title,
            'warehouse_id':warehouse_id
         },
        success: function(json) {
        if(typeof json.ask != "undefined" && json.ask){
            var html ="";
            paginationTotal = json.list.count;
            if (typeof json.list.list == "undefined") return;
            $.each(json.list.list,function(k,v){
                html +='<div class="row height25">';
                html +='<div class="cell " style="width:18%" id="product_title'+v.product_id+'" >'+v.product_title+'</div>';
				html +='<div class="cell " style="width:16%" id="product_title_cn'+v.product_id+'" >'+v.product_title_cn+'</div>';
                html +='<div class="cell " style="width:18%" id="product_sku'+v.product_id+'" >'+v.product_sku+'</div>';
                html +='<div class="cell " style="width:10%">'+v.product_status+'</div>';
                html +='<div class="cell " style="width:18%" id="poc_value'+v.product_id+'" >'+v.category.poc_value+'</div>';
                html +='<div class="cell" style="width:10%" >'+v.product_weight+'</div>';
				if($(".productRow"+v.product_id).length > 0){
					html += "<div class='cell2 ' style='width:10%'><img src='/images/yes.gif'></div>";
				}else{
					html +="<div class='cell2 ' style='width:10%'><span class='button-2' onclick='addToOrder("+v.product_id+",this)'>Add</span><input value='"+v.product_weight+"' type ='hidden' id='product_weight"+v.product_id+"'></div>";
				}
                html +="</div>";
            });
            $("#tab-div").html(html);
         }
      }
    });
}


function addToOrder(product_id,obj){
    product_title =$('#product_title'+product_id).html();
	product_title_cn = $('#product_title_cn'+product_id).html();
    product_sku =$('#product_sku'+product_id).html();
    poc_value =$('#poc_value'+product_id).html();
    product_weight =$('#product_weight'+product_id).val();

    var isExist =false;

    if($(".product_sku").length>0)
    {
        $(".product_sku").each(function(i){
            if($(this).val()==product_sku){
                isExist = true;
                return ;
            }
        });
    }


    if(isExist){
        $("#message").html("Product:"+product_sku+"was added successfully!");
        $(".messageDiv").dialog('open');
        return false;
    }

    html = '<div class="row height25 productRow'+product_id+'">';
    html +='<div class="cell" style="width:18%;align-text:center"><input type="hidden" class="inputText product_sku"  name="product_sku[]" value="'+product_sku+'" style="width:100px" readonly/> '+product_sku+'</div>';
    html +='<div class="cell" style="width:18%;align-text:center"><input type="hidden" class="inputText" name="op_description[]" value="'+product_title+'" style="width:100px"/> '+product_title+'</div>';
	
	html +='<div class="cell" style="width:15%;align-text:center"><input type="hidden" class="inputText" name="op_description_cn[]" value="'+product_title_cn+'" style="width:100px"/> '+product_title_cn+'</div>';
	
    html +='<div class="cell" style="width:18%;align-text:center">'+poc_value+'</div>';
    html +='<div class="cell" style="width:13%;align-text:center"><input type="text" class="inputText" name="op_quantity[]" value="1" style="width:80px;text-align:right" id="op_quantity'+product_id+'"  onblur="calWeihgt()"/></div>';
    html +='<div class="cell" style="width:10%;align-text:center" id="product_weight'+product_id+'">'+product_weight+'</div>';
    html +='<div class="cell no-background" style="width:8%;align-text:center"><img style="padding:5px;" src="/images/icon_del.gif" name="button'+product_sku+'" onclick="deleteProduct(this)" class="cursor" title="Delete"/><input type="hidden"  name="product_id[]" class ="product_id" value="'+product_id+'" /><input type="hidden"  name="op_id[]" value="0" /></div>';
    html +='</div>';
    $('#ProductList').append(html);
	$(obj).parent().html("<img src='/images/yes.gif'>");
	 $(".productError").hide();
	calWeihgt();
}

function deleteProduct(obj){
    $(obj).parent('div').parent('div').remove();
    calWeihgt();
}


function calWeihgt(){
	var totalQuantity =0;
    var all_weight =0;
    $('.product_id').each(function(i){
        var product_id = $(this).val();
        var op_quantity    = $('#op_quantity'+product_id).val();
        var product_weight = $('#product_weight'+product_id).html();
        var weight = parseFloat(op_quantity)*parseFloat(product_weight);
        all_weight = parseFloat(weight)+parseFloat(all_weight);
        all_weight = parseInt(all_weight*100)/100;
		totalQuantity = parseInt(totalQuantity)+parseInt(op_quantity);

    });
	if(isNaN(totalQuantity)){
		totalQuantity = 0;
	}
	if(isNaN(all_weight)){
		all_weight = 0;
	}
    $('.all_weight').html(all_weight+"&nbsp;&nbsp;").show();
	$('div.parcel_quantity').html(totalQuantity+"&nbsp;&nbsp;");
	$('input.parcel_quantity').val(totalQuantity);
	if('getCost' in window){
		getCost();
	}

}